<template>
	<!-- 图文产品编辑 -->
	<view class="bg-white fixed-bottom edit_cont">
		<u-navbar
			title="编辑详情"
			@leftClick="editBack"
			:bgColor="'#CF1526'"
			leftIconColor="#ffffff"
			titleStyle="color:#ffffff"
			:safeAreaInsetTop="true"
			:placeholder="true"
		></u-navbar>
		<view class="edit_top">
			<jinEdit
				:html="html"
				placeholder="请输入正文"
				@editOk="editOk"
				@editFocus="editFocus"
				:showImgSize="true"
				:showImgToolbar="true"
				:showImgResize="true"
				:uploadFileUrl="baseapi + '/model/common/layer_upload_img'"
				:height="editHeight + 'px'"
			></jinEdit>
		</view>
	</view>
</template>

<script>
import jinEdit from '@/components/jin-edit/jin-edit.vue';
import { baseUrl } from '@/util/api.js';
export default {
	props: ['html'],
	data() {
		return {
			baseapi: baseUrl || 'https://yunz.solarbe.com',
			screenHeight: 800,
			customBar: 44,
			editHeight: 700
		};
	},
	components: {
		jinEdit
	},
	watch: {
		editHeight(newV, oldV) {
			// console.log(newV, oldV);
			this.editHeight = newV;
		}
	},
	created() {
		this.getHeight();
	},
	methods: {
		editBack() {
			uni.showModal({
				title: '确定退出编辑？',
				content: '退出后内容不做保存',
				success: res => {
					if (res.confirm) {
						this.$emit('editBack', false);
					} else {
						this.$emit('editBack', true);
					}
				}
			});
		},
		// 点击发布
		editOk(res) {
			this.$emit('getHtml', encodeURIComponent(JSON.stringify(res.html)));
		},
		// 获取高度
		getHeight() {
			//获取当前页面的高度
			this.screenHeight = uni.getSystemInfoSync().windowHeight;
			console.log('当前页面高度', this.screenHeight);
			// 获取导航栏高度
			uni.getSystemInfo({
				success: e => {
					// this.compareVersion(e.SDKVersion, '2.5.0')
					let statusBar = 0; //状态栏高度
					let customBar = 0; // 状态栏高度 + 导航栏高度
					let navbar = 0; // 自定义标题与胶囊对齐高度

					// #ifdef MP
					statusBar = e.statusBarHeight;
					customBar = e.statusBarHeight + 45;
					if (e.platform === 'android') {
						this.$store.commit('SET_SYSTEM_IOSANDROID', false);
						customBar = e.statusBarHeight + 50;
					}
					// #endif

					// #ifdef MP-WEIXIN
					statusBar = e.statusBarHeight;
					const custom = wx.getMenuButtonBoundingClientRect();
					customBar = custom.bottom + custom.top - e.statusBarHeight;

					navbar = (custom.top - e.statusBarHeight) * 2 + custom.height;

					// #endif

					// #ifdef MP-ALIPAY
					statusBar = e.statusBarHeight;
					customBar = e.statusBarHeight + e.titleBarHeight;
					// #endif

					// #ifdef APP-PLUS
					console.log('app-plus', e);
					statusBar = e.statusBarHeight;
					customBar = e.statusBarHeight + 45;
					// #endif

					// #ifdef H5
					statusBar = 0;
					customBar = e.statusBarHeight + 45;
					// #endif

					this.customBar = customBar;
					// console.log('顶部高度', customBar);
				}
			});

			this.editHeight = this.screenHeight - this.customBar;
			// console.log('==', this.editHeight);
		},
		editFocus() {
			// 监听键盘高度
			uni.onKeyboardHeightChange(res => {
				// console.log('键盘高度', res.height);
				if (res.height == 0) {
					// this.editHeight = this.editHeight + res.height;
					this.getHeight();
				} else {
					this.editHeight = this.editHeight - res.height;
				}

				// console.log('键盘弹起', this.editHeight);
			});
		}
	}
};
</script>

<style lang="scss" scoped>
.edit_top {
}
</style>
